package com.dt.global.web.dao.articleQuery.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.springframework.stereotype.Repository;

import com.dt.components.exhibernate.impl.HibernateDaoImpl;
import com.dt.components.page.model.PageModel;
import com.dt.global.entitys.bo.TWebCoreArticle;
import com.dt.global.web.dao.Newscentrality.impl.NewscentralityImpl;
import com.dt.global.web.dao.articleQuery.ArticleQueryDao;

/**
 * 栏目新闻查询的实现类
 * @author MWG
 *
 */
@SuppressWarnings("rawtypes")
@Repository
public class ArticleQueryDaoImpl extends HibernateDaoImpl implements ArticleQueryDao{
	private static final Logger LOG = Logger.getLogger(NewscentralityImpl.class);
	@SuppressWarnings("unchecked")
	public PageModel getTWebCoreArticleChannelId(PageModel pm, String channelId)
			throws HibernateException {
		try {
			List<String> param = new ArrayList<String>();
			   //得到当期系统时间
			   SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
			   String lastModify=df.format(new Date());
		    String str="from TWebCoreArticle where ('"+lastModify+"'<=endDate or endDate is null or endDate='') and channelId=? and isDisabled='Y' order by releaseSysDate desc";
		    param.add(channelId);
			return this.findByPage(pm,str,param);
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e);
			return null;
		}
	}
	public TWebCoreArticle getTWebCoreArticleId(String articleId)
			throws HibernateException {
		try {
			List<String> param = new ArrayList<String>();
			TWebCoreArticle article =null;
		    String sql="from TWebCoreArticle where articleId=? ";
		    param.add(articleId);
			@SuppressWarnings("unchecked")
			List<TWebCoreArticle> list = this.find(sql,param);
			if(list !=null&&list.size()>0){
				article=list.get(0);
			}
			return article;
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e);
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public PageModel getTWebCoreArticleTitle(PageModel pm, String title,String pid)
			throws HibernateException {
		try {
			List<String> param = new ArrayList<String>();
			   //得到当期系统时间
			   SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
			   String lastModify=df.format(new Date());
//		    String str="from TWebCoreArticle where ('"+lastModify+"'<=endDate or endDate is null or endDate='') and title like ?  and isDisabled='Y' order by releaseSysDate desc";
		    String str = "from TWebCoreArticle t where ('"+lastModify+"'<=t.endDate or t.endDate is null or t.endDate='') and t.title like ? and t.channelId in (select t.channelId from TWebCoreArticle t where t.channelId in (select t.channelId from TWebCoreChannel t where t.nid like ?)) and t.isDisabled='Y' order by releaseSysDate desc";
		    String str1 = "from TWebCoreArticle t where ('"+lastModify+"'<=t.endDate or t.endDate is null or t.endDate='') and t.title like ? and t.channelId in (select t.channelId from TWebCoreArticle t where t.channelId in (select t.channelId from TWebCoreChannel t where t.pid <= ?)) and t.isDisabled='Y' order by releaseSysDate desc";
		    if(pid !=null && !pid.equals("")){
		        param.add("%"+title+"%");
		        param.add(pid+"%");
		        if(pid.equals("10")){
		        	return this.findByPage(pm,str1,param);	
		        }else{
		        	return this.findByPage(pm,str,param);
		        }
		    }else{
		    	return null;	
		    }
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e);
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public PageModel getdaibiaofcPageQuery(PageModel pm, String channelId)
			throws HibernateException {
		try {
			List<String> param = new ArrayList<String>();
		    String str="from TWebCoreMien where channelId=? and isDisabled='Y' order by releaseSysDate desc";
		    param.add(channelId);
			return this.findByPage(pm,str,param);
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e);
			return null;
		}
	}

}
